new Vue({
    el:'.modal-body',
    data:()=>({
        listdata:[],
        page:1,
        lang:"",
        batch:[],
        allsel:[],
        searchText:''
    }),
    mounted(){
        this.getdata()
    },
    methods:{
        getdata(){
            $.ajax({
            url:'/admin/procat/list',
            data:{
                page: this.page,
                limit: 10,
                searchText: $('#searchText').val(),
                lang: this.lang
            },
            type:'post',
            success:(data)=>{
                data = JSON.parse(data)
                this.listdata=data.product_cate_data
                this.allsel=data.product_cate_data.map(element => {
                    return element.id
                });
            }
        })
        },
        changelang(lang){
            this.lang=lang
        },
        searchfun(){
            this.getdata()
        },
        cheallsel(event){
            if($(event.target).prop('checked')){
                $(event.target).parents('table').find('input').prop('checked',true)
                this.batch=this.allsel
            }else{
                $(event.target).parents('table').find('input').prop('checked',false)
                this.batch=[]
            }
        },
        onesel(did){
            if(this.batch.indexOf(did)==-1){
                this.batch.push(did)
            }else{
                this.batch.splice(this.batch.indexOf(did),1)
            }
            var isall=true
            $('td input').each(function() {
                if(!$(this).prop('checked')){
                    isall=false
                }
            })
            $('th input').prop('checked',isall)
        },
        batchfun(type,key){
           $.ajax({
               url:'/admin/procat/changeStatus',
               type:'post',
               data:{
                id:this.batch,
                table:'procat',
                value:type,
                fun:key
               },
               success:(data)=>{
                    data = JSON.parse(data)
                    if(data.code==2000){
                        toastr.info(data.msg)
                        setTimeout(function(){
                            location.reload();
                        },1000)
                    }else{
                        toastr.warning(data.msg);
                    }
               }
           })
        },
        singlefun(did,type,key){
            $.ajax({
                url:'/admin/procat/changeStatus',
                type:'post',
                data:{
                 id:did,
                 table:'procat',
                 value:type,
                 fun:key
                },
                success:(data)=>{
                    data = JSON.parse(data)
                    if(data.code==2000){
                        toastr.info(data.msg)
                        setTimeout(function(){
                            location.reload();
                        },1000)
                    }else{
                         oastr.warning(data.msg);
                    }
                }
            })
        }
    },
    watch:{
        page:function(){
            this.getdata()
        },
        lang:function(){
            this.getdata()
        }
    }
})